#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
// 哈希+滑动窗口
class Solution
{
public:
    vector<int> findAnagrams(string s, string p)
    {
        unordered_map<char, int> count_p;
        for (auto e : p)
            count_p[e]++;
        vector<int> res;
        unordered_map<char, int> count_w;
        int r = 0;
        for (r = 0; r < p.size(); r++)
            count_w[s[r]]++;
        int l = 0;
        while (r <= s.size())
        {
            if (count_p == count_w)
                res.push_back(l);
            if (!--count_w[s[l++]])
                count_w.erase(s[l - 1]);
            if (r == s.size())
                r++;
            else
                count_w[s[r++]]++;
        }
        return res;
    }
};